/**
 * JOnAS: Java(TM) Open Application Server
 * Copyright (C) 2011 Bull S.A.S.
 * Contact: jonas-team@ow2.org
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 *
 * --------------------------------------------------------------------------
 * $Id: JndiTopicConnectionFactory.java 428 2011-08-11 16:58:31Z guillaume.sauthier $
 * --------------------------------------------------------------------------
 */


package org.osami.gateway.joram.internal;

import javax.jms.Connection;
import javax.jms.JMSException;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.TopicConnection;
import javax.jms.TopicConnectionFactory;
import javax.naming.InitialContext;

import org.apache.felix.ipojo.annotations.Component;
import org.apache.felix.ipojo.annotations.Provides;
import org.apache.felix.ipojo.annotations.ServiceProperty;
import org.apache.felix.ipojo.annotations.Validate;
import org.objectweb.joram.client.connector.ObjectFactoryImpl;

/**
 * A {@code JndiTopic} is ...
 *
 * @author Guillaume Sauthier
 */
@Component
@Provides
public class JndiTopicConnectionFactory implements TopicConnectionFactory {

    @ServiceProperty(name = "jndi.name", mandatory = true)
    private String jndiName;

    private TopicConnectionFactory delegate;

    @Validate
    public void start() throws Exception {

        // Try to obtain a InitialContext
        ClassLoader old = Thread.currentThread().getContextClassLoader();
        InitialContext initialContext = null;
        try {
            ClassLoader ourself = getClass().getClassLoader();
            Thread.currentThread().setContextClassLoader(ourself);
            initialContext = new InitialContext();
        } finally {
            Thread.currentThread().setContextClassLoader(old);
        }

        // Perform the lookup (ensure the right ClassLoader is accessible)
        try {
            ClassLoader joramClassLoader = ObjectFactoryImpl.class.getClassLoader();
            Thread.currentThread().setContextClassLoader(joramClassLoader);
            Object o = initialContext.lookup(jndiName);
            if (!(o instanceof TopicConnectionFactory)) {
                throw new IllegalArgumentException("Object returned from JNDI Name '" + jndiName + "' is not a TopicConnectionFactory. It is a " + o);
            }

            delegate = (TopicConnectionFactory) o;
        } finally {
            Thread.currentThread().setContextClassLoader(old);
        }
    }

    public TopicConnection createTopicConnection() throws JMSException {
        return delegate.createTopicConnection();
    }

    public TopicConnection createTopicConnection(String userName, String password) throws JMSException {
        return delegate.createTopicConnection(userName, password);
    }

    public Connection createConnection() throws JMSException {
        return delegate.createConnection();
    }

    public Connection createConnection(String userName, String password) throws JMSException {
        return delegate.createConnection(userName, password);
    }
}
